import { login, getUserInfo, getUserDetailById } from '@/api'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    SET_TOKEN(state, token) {
      state.token = token
      // 将token存入到 cookie
      setToken(token)
    },
    // 清空token
    REMOVE_TOKEN(state) {
      state.token = ''
      removeToken()
    },
    SET_USERINFO(state, userInfo) {
      state.userInfo = userInfo
      // console.log(state)
    }
  },
  actions: {
    async fetchLogin(store, loginForm) {
      const { data } = await login(loginForm)
      store.commit('SET_TOKEN', data)
      return data
    },
    async fetchUserInfo(store) {
      const { data } = await getUserInfo()

      // 员工基本信息
      const res = await getUserDetailById(data.userId) // id 在用户基本信息里
      // console.log(data)
      // 整合到一个 对象中
      store.commit('SET_USERINFO', { ...data, ...res.data })
    }
  }
}
